package com.test.ubivelox.thread.pool;

import java.util.LinkedList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class ATestThreadPoolActivity extends Activity {
    /** Called when the activity is first created. */
	Activity mActivity;
	Button bt1, bt2;
	TextView tv1;
	CustomSlidingDrawer sd;
	Handler mHandler = new Handler();
	Thread mThread1, mThread2;
	int x = 0;
	LinkedList<String> mStrList = new LinkedList<String>();
	LinkedList<Integer> mIntegerList = new LinkedList<Integer>();
	MyView mView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mActivity = this;
        tv1 = (TextView) findViewById(R.id.textview);
        bt1 = (Button) findViewById(R.id.button1);
//        bt2 = (Button) findViewById(R.id.tetetbt);
        bt1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mStrList.push("" + x);
				x++;
				if(x > 30){
					mStrList.push("end");
				}
			}
		});
        
//        ImageView iv = (ImageView) findViewById(R.id.slidingdrawer);
//        iv.setVisibility(View.GONE);
        
        sd = (CustomSlidingDrawer) findViewById(R.id.slidingdrawer);

//        Gallery gal = (Gallery) findViewById(R.id.gallery);
//        gal.setSpacing(10);
//		gal.setAdapter(new testGalleryAdapter(this));
        
//        bt2.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				Log.d("reavelv", "bt2 click");
//			}
//		});
        
        Gallery gal = (Gallery) sd.getContent();
        gal.setSpacing(10);
		gal.setAdapter(new testGalleryAdapter(this));
        gal.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Log.d("reavelv", "setOnTouchListener");
				return false;
			}
		});
        sd.setOnDrawerCloseListener(new CustomSlidingDrawer.OnDrawerCloseListener() {
			
			@Override
			public void onDrawerClosed() {
				Log.d("reavelv", "onDrawrClosed");
				
			}
		});
        sd.setOnDrawerOpenListener(new CustomSlidingDrawer.OnDrawerOpenListener() {
			
			@Override
			public void onDrawerOpened() {
				Log.d("reavelv", "onDrawerOpened");
				
			}
		});
		
		/*mHandler.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(!mView.isShown()){
					ATestThreadPoolActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							mView.setVisibility(View.VISIBLE);
							mHandler.postDelayed(mRunable, 300);
						}
					});
					
				}
			}
		});*/
        
        mThread1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true){
					try {
						if(mStrList.isEmpty()){
							
						}
						else{
							String str;
							Log.d("reavelv", "mStrList.size = " + mStrList.size());
							while((str = mStrList.pop()) != null){
								if(str.equals("end")){
									return;
								}
								else{
									if(mThread2 == null){
										mThread2 = new Thread(new Runnable() {
											
											@Override
											public void run() {
												while(true){
													ATestThreadPoolActivity.this.runOnUiThread(new Runnable() {
														@Override
														public void run() {
															if(!mIntegerList.isEmpty())
																tv1.setText("" + mIntegerList.pop());
														}
													});
													try {
														mThread2.sleep(300);
													} catch (InterruptedException e) {
														// TODO Auto-generated catch block
														e.printStackTrace();
													}
												}
											}
										});
										mThread2.start();
									}
									else{
										try{
											mIntegerList.push(Integer.parseInt(str));
										} catch(NumberFormatException e){
											
										}
									}
									mThread1.sleep(30);
								}
								
								
							}
						}
						Log.d("reavelv", "this = " + this);
						Log.d("reavelv", "mThread1 = " + mThread1);
						mThread1.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
        //mThread1.start();
    }
}